<?php

class prossimoClienteCLS extends Module
{
	var $connDB = 1;
	var $func;
	var $funcDB = false;
	var $sqlFunc = 1;
	var $request = array();
	var $sysError = array();
	var $appLog = 1;

	var $table = array();
	var $type = "";
	var $totRow;
	var $analyzeArray = 1;

	function loadCampagneOperatore($operatore)
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		//carico tutti i messaggi dell'utente
		$sql = "SELECT cam.id_campagna, cliente, campagna, descrizione, url
			FROM contaq_campagne AS cam
			LEFT JOIN contaq_gruppi_lavoro AS grp ON (grp.id_campagna = cam.id_campagna)
			WHERE attiva = 1 
			AND grp.operatore = $operatore ";
			
		$sql .= " ORDER BY cliente DESC, campagna DESC";
    			
// 		print $sql;	
			 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$this->request["idCampagnaLS"] = array();
		$this->request["clienteLS"] = array();
		$this->request["campagnaLS"] = array();
		$this->request["descrizioneLS"] = array();
		$this->request["urlLS"] = array();
		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["idCampagnaLS"][] = $arr["id_campagna"];
			$this->request["clienteLS"][] = $arr["cliente"];
			$this->request["campagnaLS"][] = $arr["campagna"];
			$this->request["descrizioneLS"][] = $arr["descrizione"];
			$this->request["urlLS"][] = $arr["url"];
		}
		
		return true;
	}

	function loadNextCall()
	{	
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);

		$this->session["consoleLogin"] = "";
		$this->session["idLista"] = "";
		$this->session["chiamataId"] = "";

		return true; 
	}

	function loadNextCall_old()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		//mi prendo il record dalla lista
		$sql = "SELECT contaq_get_app('".$this->session["operatore"]."',".$this->session["idCampagna"].") AS idList";
		
// 		print $sql; 
		$rs = $this->connDB->Execute( $sql );
		$this->appLog->logWrite($sql);
		
		if ( $rs === false )
		{			
		      $this->appLog->logWrite($this->connDB->ErrorMsg());
		      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
		      //print_r ($this->sysError);
		      return;
		} 

		while ( $arr = $rs->FetchRow() ) 
		{
		    $idList = $arr['idList'];		   
		}
		  
// 		print $sql;
// 		return;

		if ($idList == 0 || $idList == '' || $idList == null || $idList == false)
		{
			//print $sql;
			$this->request["typeList"] = "VERGINE";
			//mi prendo il record dalla lista
			$sql = "SELECT contaq_get_call('".$this->session["operatore"]."',".$this->session["idCampagna"].") AS idList";
			print $sql;
			$rs = $this->connDB->Execute( $sql );
			$this->appLog->logWrite($sql);
			
			if ( $rs === false )
			{			
			      $this->appLog->logWrite($this->connDB->ErrorMsg());
			      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			      //print_r ($this->sysError);
			      return;
			} 

			while ( $arr = $rs->FetchRow() ) 
			{
			    //$idList = str_replace('idList','',$rs);
			    //$idList = trim ($idList);
			    $idList = $arr['idList'];		   
			}
		}
		else 
		{
			    $this->request["typeList"] = "APPUNTAMENTO";
		}
		
		

		if ($idList == 0 || $idList == '' || $idList == null || $idList == false) return false;

		$sql = "SELECT contaq_create_call($idList , '".$this->session["operatore"]."',".$this->session["idCampagna"].") AS callId";
		
		print $sql;
		
		$rs = $this->connDB->Execute( $sql );
		$this->appLog->logWrite($sql);

		if ( $rs === false )
		{			
		      $this->appLog->logWrite($this->connDB->ErrorMsg());
		      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
		      //print_r ($this->sysError);
		      return;
		} 

		while ( $arr = $rs->FetchRow() ) 
		{
		    $chiamataId = $arr['callId'];
		}
		//print ">$chiamataId<";

	    
		if ($chiamataId == 0 || $chiamataId == '' || $chiamataId == null || $chiamataId == false)
		    return false;
		
		$this->session["idLista"] = $idList;
		$this->session["chiamataId"] = $chiamataId;
    
		return true; 
	}  

	function interfaceAgentInsert($comando,$parametri,$flag)
	{		
		$sql = "INSERT INTO dialer_interfaccia (comando,parametri,flageseguito)
			VALUES ('$comando','$parametri',$flag)";
		
// 		print $sql;
// 		exit;
		$rs = $this->connDB->Execute( $sql );
		$this->appLog->logWrite($sql);

		if ( $rs === false )
		{			
		      $this->appLog->logWrite($this->connDB->ErrorMsg());
		      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
		      //print_r ($this->sysError);
		      return;
		} 
		
		//print ">$chiamataId<";

	    return true;
	}

	function interfaceAgentCheck($comando,$parametri,$flag)
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
	
		//funzione che a seconda dei tre parametri restituisce l'id del recont della tabella di controllo
		$sql = "SELECT id,parametri
			FROM dialer_interfaccia 
			WHERE comando = '$comando' 
			AND parametri like '$parametri' 
			AND flageseguito in($flag)";
    			
//  		print $sql;	
// 		exit;	 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$idInterfaccia = '';

		while ( $arr = $rs->FetchRow() ) 
		{	
			$idInterfaccia = $arr["id"];
		}
		
		return $idInterfaccia;
		
	}

	function interfaceAgentCheckCall()
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
		
		//carico tutti i messaggi dell'utente
		$sql = "SELECT id,parametri
			FROM dialer_interfaccia 
			WHERE comando = 'AgentState' 
			AND parametri like 'SIP/{$this->session["operatore"]}|6|{$this->session["idCampagna"]}|%' 
			AND flageseguito = 2";
    			
//  		print $sql;	
// 		exit;	 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$this->request["parametri"] = '';
		$this->request["idInterfaccia"] = '';

		while ( $arr = $rs->FetchRow() ) 
		{	
			$this->request["parametri"] = $arr["parametri"];
			$this->session["idInterfaccia"] = $arr["id"];
		}
		
		
		$arrPar = split('\|',$this->request["parametri"]);
		
// 		print $arrPar[3];	
		if ($arrPar["3"] != "")
		{
			$sql = "select t2.telefono, t3.telefono_per_ricontattare
					from contaq_chiamate t1
					inner join contaq_liste t3 on (t3.id_lista = t1.id_lista)
					left join contaq_telefoni t2 on (t2.id_telefono = t1.id_telefono)
					where t1.chiamata_id = '{$arrPar[3]}'";
					
// 	  		print $sql;	
	// 		exit;	 
			$rs = $this->connDB->Execute( $sql );
			
			$this->appLog->logWrite($sql);
			if ( $rs === false )
			{			
				$this->appLog->logWrite($this->connDB->ErrorMsg());
				$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
				print_r ($this->sysError);
				return;
			}
			
			$this->session["telefono"] = '';
			$telefono = "";
			$recallTell = "";
			while ( $arr = $rs->FetchRow() ) 
			{	
// 				$this->session["telefono"] = substr($arr["telefono"],'3');
				$telefono = $arr["telefono"];
				$recallTell = $arr["telefono_per_ricontattare"];
			}
			
			if ($recallTell != "") $this->session["telefono"] = $recallTell;
			else $this->session["telefono"] = $telefono;
		

		}
		
		return $arrPar[3];
	}

	function interfaceAgentUpdate($idInterfaccia,$flag)
	{		
		$sql = "UPDATE dialer_interfaccia 
				SET flageseguito = $flag
				WHERE id = $idInterfaccia";
		
// 		print $sql;
// 		exit;
		$rs = $this->connDB->Execute( $sql );
		$this->appLog->logWrite($sql);

		if ( $rs === false )
		{			
		      $this->appLog->logWrite($this->connDB->ErrorMsg());
		      $this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
		      //print_r ($this->sysError);
		      return;
		} 
	
		//reimposto idInterfaccia vuoto perche la chiamata è stata chiusa e quindi non deve piu rientrare
		$this->session["idInterfaccia"] = "";
		
		//print ">$chiamataId<";

	    return true;
	}

	function checkEsitoDialer($chiamataId)
	{
		$this->connDB->SetFetchMode(ADODB_FETCH_ASSOC);
	
		//funzione che a seconda dei tre parametri restituisce l'id del recont della tabella di controllo
		$sql = "SELECT esito_dialer from contaq_chiamate where chiamata_id ='$chiamataId'";
    			
//  		print $sql;	
// 		exit;	 
		$rs = $this->connDB->Execute( $sql );
		
		$this->appLog->logWrite($sql);
		if ( $rs === false )
		{			
			$this->appLog->logWrite($this->connDB->ErrorMsg());
			$this->sysError[] = "Error DB : ".$this->connDB->ErrorMsg();
			print_r ($this->sysError);
			return;
		}
		
		$checkEsito = '';

		while ( $arr = $rs->FetchRow() ) 
		{	
			$checkEsito = $arr["esito_dialer"];
		}
		
		return $checkEsito;
		
	}
}
		
?>